﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    public sealed partial class MainMenuState : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Menu menu;

        private void buttonNewGame()
        {
            StateManager.currentState = States.PLAYGAME;
            if (GameVariables.music)
            {
                MediaPlayer.Stop();
                MediaPlayer.Play(ResourceManager.getSong("ingameSong"));
            }
        }

        private void buttonOptions()
        {
            StateManager.currentState = States.OPTIONS;
        }

        private void buttonQuit()
        {
            Game.Exit();
        }

        public MainMenuState(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.spriteBatch = spriteBatch;

            ButtonContainer buttons = new ButtonContainer();

            buttons.AddButton(ResourceManager.getTexture("buttonNewGame"),
                                new ButtonEvent(buttonNewGame), 
                                Color.White,
                                Color.Orange, 
                                Color.Red);
            buttons.AddButton(ResourceManager.getTexture("buttonOptions"),
                                new ButtonEvent(buttonOptions),
                                Color.White,
                                Color.Orange,
                                Color.Red);
            buttons.AddButton(ResourceManager.getTexture("buttonQuit"),
                                new ButtonEvent(buttonQuit),
                                Color.White,
                                Color.Orange,
                                Color.Red);


            menu = new Menu(game, buttons, 297, 52);
        }

        public sealed override void Update(GameTime gameTime)
        {
            if (StateManager.currentState == States.MAINMENU)
            {
                menu.Update(gameTime);
            }
        }

        public sealed override void Draw(GameTime gameTime)
        {
            if (StateManager.currentState == States.MAINMENU)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(ResourceManager.getTexture("Loading"), Game.GraphicsDevice.Viewport.TitleSafeArea, Color.White);
                spriteBatch.DrawString(ResourceManager.getFont("JingJing"), "Main Menu", new Vector2(50, Game.GraphicsDevice.Viewport.Height - 120), Color.Orange);
                spriteBatch.End();
                menu.Draw(spriteBatch);
            }
        }
    }
}